#include <glib.h>

static void _ptr_array_foreach_print_func(gpointer data, gpointer user_data)
{
    g_print("%s: %s \n", (gchar *)user_data, (gchar *)data);
}

static void _ptr_array_free_func(gpointer data)
{
    if(NULL != data) {
        g_free(data);
    }
}

static gint _ptr_array_str_cmp_func(gconstpointer a, gconstpointer b)
{
    gchar *str_a = NULL;
    gchar *str_b = NULL;

    str_a = (gchar *)a;
    str_b = (gchar *)b;

    return g_strcmp0(str_a, str_b);
}

gint main(gint argc, gchar **argv)
{
    GPtrArray *parr = NULL;

    gchar str0[8] = {0};
    gchar str1[8] = {0};
    gchar str2[8] = {0};
    gchar *str[3] = {NULL};
    
    g_strlcpy(str0, "AAA", 8);
    g_strlcpy(str1, "BBB", 8);
    g_strlcpy(str2, "CCC", 8);

    str[0] = g_strdup(str0);
    str[1] = g_strdup(str1);
    str[2] = g_strdup(str2);

    parr = g_ptr_array_sized_new(10);
    
    g_ptr_array_add(parr, (gpointer)str[0]);
    g_ptr_array_add(parr, (gpointer)str[2]);
    g_ptr_array_add(parr, (gpointer)str[1]);

    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "ori");

    g_ptr_array_set_free_func(parr, _ptr_array_free_func);

    g_ptr_array_sort(parr, _ptr_array_str_cmp_func);

    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "sort");

    g_ptr_array_free(parr, TRUE);

    return 0;
}